import {
	validatenull
} from '@/util/validate.js'
import {
	calcDate
} from '@/util/date.js';

import config from '@/common/config'

module.exports = {
	data() {
		return {
			refreshLock: false,
			mainBtnColor: 'linear-gradient(to right, #ff7900, #fbcd4a)',
			tabPageStyle: 'padding-bottom: 70rpx; box-sizing: border-box;'
		}
	},
	props: {

	},
	// 分享
	onShareAppMessage(res) {
		return {
			title: '倾听您的声音，响应您的期待',
			path: '/pages/mainPage/homePage/homePage',
			// imageUrl: require('@/static/image/share-image.jpeg')
			imageUrl: `${this.imageDomain}/share-image.jpeg`
		}
	},
	onShareTimeline() {
		return {
			title: '山推客户倾听中心'
		}
	},
	onShow() {
	},
	mounted() {},
	computed: {},
	methods: {
		// TODO
		showNotOpenFeature(type) {
			// uni.$u.toast(typeof msg === 'string' ? msg : '功能暂未开通')
			this.$u.route({
				url: '/pages/construction/construction',
				type: type || 'to'
			})
		},
		// uni 内置
		hideKeyboard() {
			uni.hideKeyboard()
		},
		// 位置相关
		getLocation(location) {
			return new Promise((resolve, reject) => {
				this.$u.amapPlugin.getRegeo({
					location,
					success: (data) => {
						resolve(data)
					},
					fail: (err) => {}
				})
			})
		},
		calcTokenTime(time) {
			return calcDate(time || this.tokenTime, new Date().getTime());
		},
		// 登录相关
		updateToken() {
			if (this.refreshTime) {
				clearInterval(this.refreshTime)
			}
			this.refreshTime = setInterval(() => {
				const date = this.calcTokenTime()
				if (validatenull(date)) return;
				if (date.seconds >= config.tokenTime && !this.refreshLock) {
					this.refreshLock = true;
					this.$store.dispatch('RefreshToken', {
						vm: this
					})
						.then(() => {
							this.refreshLock = false;
						})
						.catch(() => {
							this.refreshLock = false;
						});
				}
			}, 10000);
		},
		loadUserInfo() {
			return this.$u.api['user/getUserInfo']().then(res => {
				this.$u.vuex('userInfo', Object.assign(this.userInfo, res))
			})
		},
		afterLogin(data) {
			this.$u.vuex('refreshToken', data
				.refresh_token)
			this.$u.vuex('accessToken', data
				.access_token)
			this.$u.vuex('tokenTime',
				new Date().getTime())
			return new Promise((resolve, reject) => {
				this.initData().then(() => {
					resolve()
				})
			})

		},
		loginByOpenId({
			openId,
			redirect,
			redirectUrl,
			redirectOptions,
			wxInfo,
			promotionId
		}) {
			return new Promise((resolve, reject) => {
				this.$u.api['user/loginByWechat']({
					openId,
					phone: wxInfo.phone,
					avatar: wxInfo.avatarUrl,
					nickName: wxInfo.nickName,
					countryCode: wxInfo.countryCode,
					promotionId
				}).then(data => {
					this.$u.vuex('userInfo', Object.assign({
						wxInfo
					}, data))
					this.afterLogin(data).then(res => {
						// 完善资料大转盘活动
						if (!this.userInfo.realName && !redirectUrl) {
							redirectUrl = '/pages/usercenter/userinfo/userinfo'
						}
						
						
						if (redirect) {
							if (redirectUrl) {
								this.$u.route({
									url: redirectUrl,
									params: redirectOptions
								})
							} else {
								this.$u.route({
									url: '/pages/mainPage/homePage/homePage',
									type: 'tab'
								})
							}
						}
						resolve(data)
					})

				})
			})
		},
		logOut() {
			this.$u.vuex('userInfo', '')
			this.$u.vuex('refreshToken', '')
			this.$u.vuex('accessToken', '')
			this.$u.vuex('tokenTime', '')
			this.$u.vuex('openId', '')

			this.$u.route({
				url: '/pages/mainPage/homePage/homePage',
				type: 'tab'
			})
		},
		initData() {
			return new Promise((resolve, reject) => {
				this.updateToken()
				const dmsPromise = this.$u.api['user/checkBindDms']().then(res => {
					this.$u.vuex('isBindDms', res)
				})
				let promiseArr = [this.loadUserInfo(), dmsPromise]
				Promise.all(promiseArr).then(() => {
					resolve()
				})
			})

		},

		checkUpdateVersion() {
			if (uni.canIUse('getUpdateManager')) {
				//创建 UpdateManager 实例
				const updateManager = uni.getUpdateManager();
				console.log('是否进入模拟更新');
				updateManager.onCheckForUpdate(function(res) {
					console.log('是否获取版本');
					if (res.hasUpdate) {
						updateManager.onUpdateReady(function() {
							updateManager.applyUpdate();
						})
						updateManager.onUpdateFailed(function() {
							uni.showModal({
								title: '已经有新版本喽~',
								content: '请您删除当前小程序，到微信 “发现-小程序” 页，重新搜索打开哦~',
							})
						})
					}
				})
			} else {
				uni.showModal({
					title: '溫馨提示',
					content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
				})
			}
		},

	}
}
